/**
 * 
 */
package com.cuc.platform.core.service.logic;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.cuc.platform.core.componet.security.utils.CommonIdentityTools;
import com.cuc.platform.core.dao.baseInfo.OrganizationDao;
import com.cuc.platform.core.domain.baseInfo.Organization;
import com.cuc.platform.core.service.OrganizationService;
import com.cuc.platform.core.service.ServiceException;

/**
 * @author peepul
 *	
 * 组织结构业务实现类
 */
public class OrganizationServiceImpl implements OrganizationService {

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.OrganizationService#getOrg(java.lang.Long)
	 */
	@Override
	public Organization getOrg(Long id) {
		if(null == id) return null;
		
		return organizationDao.getOrg(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.OrganizationService#getAllOrgs()
	 */
	@Override
	public List<Organization> getAllOrgs() {
		return organizationDao.getAllOrgs();
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.OrganizationService#insertOrg(com.cuc.platform.core.domain.Organization)
	 */
	@Override
	public void insertOrg(Organization org) throws ServiceException {
		if(null == org) return;
		
		//赋缺省值
		if(null == org.getStatus()){
			org.setStatus(CommonIdentityTools.IS_ACTIVE);
		}
		
		organizationDao.insertOrg(org);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.OrganizationService#updateOrg(com.cuc.platform.core.domain.Organization)
	 */
	@Override
	public void updateOrg(Organization org) throws ServiceException {
		if(null == org) return;
		
		//赋缺省值
		if(null == org.getStatus()){
			org.setStatus(CommonIdentityTools.IS_ACTIVE);
		}
		
		organizationDao.updateOrg(org);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.OrganizationService#delOrg(java.lang.Long)
	 */
	@Override
	public void delOrg(Long id) throws ServiceException {
		if(null == id) return;
		
		try{
			int rv = organizationDao.delOrg(id);
			
			//如果返回值为0,则没找到对应id的数据，抛出业务异常给web层
			if(rv == 0){
				logger.warn("没找到id为：" + id + "的数据，无效的删除操作。");
				throw new ServiceException("系统中不存在要删除的数据！");
			}
		}catch(DataAccessException ex){
			logger.error("有外键关联的数据，无效的删除操作。");
			throw new ServiceException("请先删除关联数据，才能删除本条数据。");
		}
	}
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.OrganizationService#updateStatus(java.lang.Long, java.lang.Integer)
	 */
	@Override
	public void updateStatus(Long orgId, Integer status) throws ServiceException {
		if((null == orgId) || (null == status)) return;
		
		Organization org = new Organization();
		org.setOrgId(orgId);
		org.setStatus(status);
		
		organizationDao.updateStatus(org);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.OrganizationService#getChildOrgs(java.lang.Long)
	 */
	@Override
	public List<Organization> getChildOrgs(Long parentId) {
		if(null == parentId) return null;
		
		return organizationDao.getChildOrgs(parentId);
	}
	
	//日志
	private static Logger logger = Logger.getLogger(OrganizationServiceImpl.class);
	
	/**
	 * Ioc Beans
	 */
	private OrganizationDao organizationDao;

	/**
	 * @param organizationDao the organizationDao to set
	 */
	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}
}
